package com.prs.crm.action.stock;

import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.crm.Customer;
import com.prs.crm.domain.stock.Product;
import com.prs.crm.domain.stock.ProductClass;
import com.prs.crm.domain.stock.ProductStore;
import com.prs.crm.domain.stock.ProductUnit;
import com.prs.crm.domain.stock.StockAlert;
import com.prs.crm.domain.store.StoreRoom;
import com.prs.crm.domain.sys.Code;
import com.prs.crm.domain.sys.CodeCommon;
import com.prs.crm.service.crm.CustomerService;
import com.prs.crm.service.stock.ProductClassService;
import com.prs.crm.service.stock.ProductService;
import com.prs.crm.service.stock.ProductStoreService;
import com.prs.crm.service.stock.ProductUnitService;
import com.prs.crm.service.stock.StockAlertService;
import com.prs.crm.service.stock.StoreRoomService;
import com.prs.crm.service.sys.CodeCommonService;
import com.prs.crm.service.sys.CodeService;

public class ProductAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private int roomid;

	private StoreRoomService storeRoomService;

	private List<StoreRoom> storeRooms;

	private ProductClassService productClassService;

	private ProductService productService;

	private PaginationSupport pagination;

	private ProductClass productClass;

	private Product product;

	private String[] attrvals;

	private Integer[] attrs;
	
	private String[] storeNum;
	
	private ProductStore productStore;
	
	private Collection<ProductStore> productStores;

	private Collection<StockAlert> stockAlerts;

	private StockAlertService stockAlertService;

	private String[] maxNumbers;

	private String[] minNumbers;

	private StoreRoom[] storerooms;

	private StockAlert[] stockAlerts1;

	private Collection<ProductUnit> productUnits;

	private ProductUnitService productUnitService;
    private ProductStoreService productStoreService;
	private String batchNumber;
	private String listString;
	private String state = "a";

	private CodeCommon codeCommon;
	private CodeCommonService codeCommonService;
	private String Encode;
	private Code code;
	private CodeService codeService;
	private Collection<CodeCommon> codelist;
	private CustomerService customerService;
	public CustomerService getCustomerService() {
		return customerService;
	}

	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

	public Collection<CodeCommon> getCodelist() {
		return codelist;
	}

	public void setCodelist(Collection<CodeCommon> codelist) {
		this.codelist = codelist;
	}

	public CodeCommon getCodeCommon() {
		return codeCommon;
	}

	public void setCodeCommon(CodeCommon codeCommon) {
		this.codeCommon = codeCommon;
	}

	public CodeCommonService getCodeCommonService() {
		return codeCommonService;
	}

	public void setCodeCommonService(CodeCommonService codeCommonService) {
		this.codeCommonService = codeCommonService;
	}

	public String getEncode() {
		return Encode;
	}

	public void setEncode(String encode) {
		Encode = encode;
	}

	public Code getCode() {
		return code;
	}

	public void setCode(Code code) {
		this.code = code;
	}

	public CodeService getCodeService() {
		return codeService;
	}

	public void setCodeService(CodeService codeService) {
		this.codeService = codeService;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getListString() {
		return listString;
	}

	public void setListString(String listString) {
		this.listString = listString;
	}

	public ProductUnitService getProductUnitService() {
		return productUnitService;
	}

	public void setProductUnitService(ProductUnitService productUnitService) {
		this.productUnitService = productUnitService;
	}

	public Collection<ProductUnit> getProductUnits() {
		return productUnits;
	}

	public void setProductUnits(Collection<ProductUnit> productUnits) {
		this.productUnits = productUnits;
	}

	public StockAlert[] getStockAlerts1() {
		return stockAlerts1;
	}

	public void setStockAlerts1(StockAlert[] stockAlerts1) {
		this.stockAlerts1 = stockAlerts1;
	}

	public String[] getMaxNumbers() {
		return maxNumbers;
	}

	public void setMaxNumbers(String[] maxNumbers) {
		this.maxNumbers = maxNumbers;
	}

	public String[] getMinNumbers() {
		return minNumbers;
	}

	public void setMinNumbers(String[] minNumbers) {
		this.minNumbers = minNumbers;
	}

	public StoreRoom[] getStorerooms() {
		return storerooms;
	}

	public void setStorerooms(StoreRoom[] storerooms) {
		this.storerooms = storerooms;
	}

	public Collection<StockAlert> getStockAlerts() {
		return stockAlerts;
	}

	public void setStockAlerts(Collection<StockAlert> stockAlerts) {
		this.stockAlerts = stockAlerts;
	}

	public StockAlertService getStockAlertService() {
		return stockAlertService;
	}

	public void setStockAlertService(StockAlertService stockAlertService) {
		this.stockAlertService = stockAlertService;
	}

	public void setProductClassService(ProductClassService productClassService) {
		this.productClassService = productClassService;
	}

	public ProductClassService getProductClassService() {
		return productClassService;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public ProductService getProductService() {
		return productService;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public Product getProduct() {
		return product;
	}

	public void setAttrs(Integer[] attrs) {
		this.attrs = attrs;
	}

	public Integer[] getAttrs() {
		return attrs;
	}

	public void setAttrvals(String[] attrvals) {
		this.attrvals = attrvals;
	}
    
	public String[] getStoreNum() {
		return storeNum;
	}

	public void setStoreNum(String[] storeNum) {
		this.storeNum = storeNum;
	}

	public ProductStore getProductStore() {
		return productStore;
	}

	public void setProductStore(ProductStore productStore) {
		this.productStore = productStore;
	}

	public String[] getAttrvals() {
		return attrvals;
	}

	public void setProductClass(ProductClass productClass) {
		this.productClass = productClass;
	}

	public ProductClass getProductClass() {
		return productClass;
	}
    
	public Collection<ProductStore> getProductStores() {
		return productStores;
	}

	public void setProductStores(Collection<ProductStore> productStores) {
		this.productStores = productStores;
	}

	public int getRoomid() {
		return roomid;
	}

	public void setRoomid(int roomid) {
		this.roomid = roomid;
	}

	public StoreRoomService getStoreRoomService() {
		return storeRoomService;
	}

	public void setStoreRoomService(StoreRoomService storeRoomService) {
		this.storeRoomService = storeRoomService;
	}

	public List<StoreRoom> getStoreRooms() {
		return storeRooms;
	}

	public void setStoreRooms(List<StoreRoom> storeRooms) {
		this.storeRooms = storeRooms;
	}

	@Action("listProduct")
	public String listProduct() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Product.class);
		this.setPagination(this.getProductService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action("listProductChange")
	public String listProductChange() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Product.class);
		this.setPagination(this.getProductService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action("selectProduct")
	public String selectProduct() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Product.class);
		this.setPagination(this.getProductService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action("selectProductStoreIn")
	public String selectProductStoreIn() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Product.class);

		this.setPagination(this.getProductService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action("selectProductInventory")
	public String selectProductInventory() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Product.class);
		this.setPagination(this.getProductService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action("editProduct")
	public String editProduct() {
		if (productClass == null && product != null) {
			this.setProductClass(product.getClassifi());
		}
		/*
		 * storeRooms = this.getStoreRoomService().getAllstoreroom(); if
		 * (product != null) { this.setStockAlerts(this.getStockAlertService()
		 * .getStockAlertsByProduct(product)); }
		 */
		this.codelist = this.getCodeCommonService().getCodeCommonByCodeType(
				"产品");
		this.setProductUnits(this.getProductUnitService().getAllproductUnits());
		return SUCCESS;
	}

	@Action("createProduct")
	public String createProduct() {
		return this.editProduct();
	}

	@Action("viewProduct")
	public String viewProudct() {
		productClass = this.product.getClassifi();
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "saveNewProduct", results = { @Result(name = "success", type = "redirect", location = "createProduct") })
	public String saveNewProduct() {
		this.getProductService().saveProduct(product, attrs, attrvals);

		this.setPrompt("操作成功");
		return SUCCESS;
	}

	@Action(value = "saveProduct", results = {
			@Result(name = "success", type = "redirect", location = "listProduct"),
			@Result(name = "error", type = "chain", location = "editProduct") })
	public String saveProduct() {
		state = "b";
		if (product.getClassifi() == null) {
			this.addActionError("产品分类不能为空");
			return ERROR;
		}
		if (product.getNorm() == null || product.getNorm().equals("")) {
			this.addActionError("产品规格不能为空");
			return ERROR;
		}
		if (product.getName() == null || product.getName().equals("")) {
			this.addActionError("产品名称不能为空");
			return ERROR;
		}
		if (product.getPlanPrice() == null) {
			this.addActionError("产品计划价格不能为空");
			return ERROR;
		}
		if (product.getPrice() == null) {
			this.addActionError("产品市场价格不能为空");
			return ERROR;
		}
		if (product.getProductUnit() == null) {
			this.addActionError("产品计量值不能为空");
			return ERROR;
		}
		if (product.getProductcode() == null
				|| product.getProductcode().equals("")) {
			this.addActionError("编码不能为空");
			return ERROR;
		}
		product.setAlert(false);
		
		if (product.getId() != null) {
			this.getProductService().saveProduct(product, attrs, attrvals);
		} else {
			if ("x".equals(Encode) || "".equals(Encode)) {
				this.addActionError("请选择编码类型");
				return ERROR;
			}
			if ("n".equals(Encode)) {
				int EncodeRplice = this.getCodeService().getBackRpliceAll("手动编码",
						"产品", product.getProductcode());
				if (EncodeRplice != 0) {
					this.addActionError("编码重复!请重新输入");
					return ERROR;
				}
			}
			if ("n".equals(Encode)) {
				Code codes = new Code();
				codes.setCodeValue(product.getProductcode());
				codes.setEntityName(this.getClass().getName());
				codes.setCodeType("手动编码");
				codes.setCodeMolde("产品");
				this.getCodeService().save(codes);
				product.setCode(codes);
				this.product.setUnit(this.product.getProductUnit()
						.getUnitName());
				this.getProductService().saveProduct(product, attrs, attrvals);
			} else {
				CodeCommon Vob = this.getCodeCommonService().getVobject(
						codeCommon.getId());
				Code codes = new Code();
				codes.setEntityName(this.getClass().getName());
				codes.setCodeValue(product.getProductcode());
				Vob.setNumBackUp(Increase(Vob.getNumBackUp()));
				this.codeCommonService.saveOrUpdate(Vob);
				codes.setCodeType(Vob.getCodePlan());
				codes.setCodeMolde("产品");
				this.getCodeService().save(codes);
				product.setCode(codes);
				this.product.setUnit(this.product.getProductUnit()
						.getUnitName());
				this.getProductService().saveProduct(product, attrs, attrvals);
			}
		}

		return SUCCESS;
	}

	@Action("listProductByStoreAlert")
	public String listProductByStoreAlert() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Product.class);
		this.setPagination(this.getProductService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action(value = "saveStoreAlert", results = { @Result(name = "success", type = "redirect", location = "listProductByStoreAlert") })
	public String saveStoreAlert() {
		try {

			this.getStockAlertService().saveStockAlert(this.getStockAlerts1(),
					product, storerooms, maxNumbers, minNumbers);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return SUCCESS;
	}

	@Action("editStoreAlert")
	public String editStoreAlert() {
		this.storeRooms = this.getStoreRoomService().getAllstoreroom();
		this.setProductStores(this.getProductStoreService()
				.getAllProductStores());
		
		if (product != null) {
		    this.setStockAlerts(this.getStockAlertService().getStockAlertsByProduct(product));
		    if(!stockAlerts.isEmpty()){
		      product.setAlert(true);
		      this.getProductService().save(product);
		    }
		}
		
		return SUCCESS;
	}
	@Action("viewStoreAlert")
	public String viewStoreAlert() {
		this.storeRooms = this.getStoreRoomService().getAllstoreroom();
		this.setProductStores(this.getProductStoreService()
				.getAllProductStores());
		
		if (product != null) {
						this.setStockAlerts(this.getStockAlertService()
					.getStockAlertsByProduct(product));
		}
		return SUCCESS;
	}

	@Action("selectCustomer")
	public String selectCustomer() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Customer.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getCustomerService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	public String getBatchNumber() {
		return batchNumber;
	}

	public void setBatchNumber(String batchNumber) {
		this.batchNumber = batchNumber;
	}
	
	

	public ProductStoreService getProductStoreService() {
		return productStoreService;
	}

	public void setProductStoreService(ProductStoreService productStoreService) {
		this.productStoreService = productStoreService;
	}

	public static String Increase(String backupNum) {
		String Inc = "";
		int lengths = backupNum.length();
		String lengthss = (Integer.parseInt(backupNum) + 1) + "";
		int increase = (lengthss.trim()).length();
		if (lengths != increase) {
			String In = "";
			for (int i = 0; i < lengths - increase; i++) {
				In = In + "0";
			}
			In = (In + lengthss).trim();
			Inc = In;
		} else {
			Inc = lengthss;
		}
		return Inc.trim();
	}

}
